package com.cdy.demo.algorithm;

public class Leetcode529 {
	public static void main(String[] args) {
		System.out.println((int)'0');
		System.out.println((int)'9');
		System.out.println(new Leetcode529().updateBoard(new char[][]
				{{'E','E','E','E','E','E','E','E'}
				,{'E','E','E','E','E','E','E','M'}
				,{'E','E','M','E','E','E','E','E'}
				,{'M','E','E','E','E','E','E','E'}
				,{'E','E','E','E','E','E','E','E'}
				,{'E','E','E','E','E','E','E','E'}
				,{'E','E','E','E','E','E','E','E'}
				,{'E','E','M','M','E','E','E','E'}}, new int[]{0,0}));

	}
	int[] X = new int[]{-1,0,1};
	int[] Y = new int[]{-1,0,1};



	public char[][] updateBoard(char[][] board, int[] click) {
		int x = click[0];
		int y = click[1];
		if (board[x][y] == 'B') {
			return board;
		}
		if (board[x][y] >= '0' && board[x][y] <= '9') {
			return board;
		}
		if (board[x][y] == 'M') {
			board[x][y] = 'X';
			return board;
		} else {
			char num = '0';
			for (int k : X) {
				for (int j : Y) {
					if (k == 0 && j == 0) {
						continue;
					}
					if (x + k < 0 || x+k >= board.length) {
						continue;
					}
					if (y + j < 0 || y+j >= board[0].length) {
						continue;
					}
					if (board[x + k][y + j] == 'M') {
						num++;
					}
				}
			}
			if (num == '0') {
				board[x][y] = 'B';
				for (int k : X) {
					for (int j : Y) {
						if (k == 0 && j == 0) {
							continue;
						}
						if (x + k < 0 || x+k >= board.length) {
							continue;
						}
						if (y + j < 0 || y+j >= board[0].length) {
							continue;
						}
						if (board[x + k][y + j] == 'M') {
							continue;
						}
						updateBoard(board, new int[]{x + k, y + j});
					}
				}
			} else {
				board[x][y] = num;
			}
			return board;
		}
	}
}
